package com.woniu.his.bms.app.apiImpl;

import cn.hutool.core.bean.BeanUtil;
import com.alipay.api.AlipayApiException;
import com.woniu.his.bms.client.BillRecordClient;
import com.woniu.his.bms.client.result.BillRecordResult;
import com.woniu.his.bms.common.BmsResultCode;
import com.woniu.his.bms.service.BillsRecordService;
import com.woniu.his.bms.service.dto.BillRecordDto;
import com.woniu.his.exception.HisException;
import com.woniu.his.result.Result;
import lombok.extern.java.Log;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.List;

/**
 * @version: java version 1.8
 * @Author: ZJH
 * @description:
 * @date: 2022-11-30 16:31
 */
@RestController
@RequestMapping("bms")
@Slf4j
public class BillRecordApiImpl implements BillRecordClient {

    @Resource
    private BillsRecordService billsRecordService;

    @Resource
    private RocketMQTemplate rocketMQTemplate;

    @Override
    public Result<BillRecordResult> createBillRecord(String billType, BigDecimal amount) {
        log.info("创建账单参数: {} {}",billType,amount);
        BillRecordDto billRecord = billsRecordService.createBillRecord(billType,amount);
        BillRecordResult billRecordResult = BeanUtil.copyProperties(billRecord, BillRecordResult.class);
        return Result.getInstance(BillRecordResult.class).setData(billRecordResult);
    }

    @Override
    public Result<BillRecordResult> createBillRecord(String billType,BigDecimal amount ,Long caseHistoryId) {
        log.info("创建账单参数: {} {} {}",billType,caseHistoryId,caseHistoryId);
        BillRecordDto billRecord = billsRecordService.createBillRecord(billType,amount,caseHistoryId);
        BillRecordResult billRecordResult = BeanUtil.copyProperties(billRecord, BillRecordResult.class);
        return Result.getInstance(BillRecordResult.class).setData(billRecordResult);
    }

    @Override
    public Result<List<BillRecordResult>> queryBillRecord(Long caseHistoryId) {
        log.info("查询未支付账单病历号: {}",caseHistoryId);
        List<BillRecordDto> billRecordDtos = billsRecordService.queryBillRecord(caseHistoryId);
        List<BillRecordResult> results = BeanUtil.copyToList(billRecordDtos, BillRecordResult.class);
        return Result.getInstanceList(BillRecordResult.class).setData(results);
    }

    @Override
    public Result<String> billPay(Long billId) throws IOException {
        log.info("账单支付参数账单id：{}",billId);
        String pay = null;
        try {
            pay = billsRecordService.pay(billId);
        } catch (AlipayApiException e) {
            throw new HisException(BmsResultCode.PAY_DEFEAT);
        }
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = sra.getResponse();
        if(pay != null) {
            Message<Long> message = MessageBuilder.withPayload(billId).build();
            rocketMQTemplate.syncSend("food-find-pay", message,5000,5);
            response.setCharacterEncoding("utf8");
            response.setContentType("text/html;charset=utf8");
            response.getWriter().write(pay);
            response.getWriter().flush();
        } else {
            throw new HisException(BmsResultCode.PAY_DEFEAT);
        }
        return Result.getInstance(String.class).setData(pay);
    }

    @Override
    public void paySuccess() {
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = sra.getRequest();
        String trade_status = request.getParameter("trade_status");
        if(trade_status!=null&&trade_status.equals("TRADE_SUCCESS")){
            String num = request.getParameter("out_trade_no");
            billsRecordService.paySuccess(num);
        }
    }

}
